﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.Model;
using AspNetCs.IDAL;
using System.Data;

namespace AspNetCs.SQLServerDAL
{
    /// <summary>
    /// 站内消息
    /// </summary>
    public class Message_DAL:IMessage
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(MessageModel model)
        {
            int count = 0;
            model.SendTime = DateTime.Now;
            model.IsView = 0;
            model.ViewTime = DateTime.Now;

            db.ParameterArry[] paras = new db.ParameterArry[11];

            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@SenderId";
            paras[0].objValue = model.SenderId;

            paras[1].dbType = SqlDbType.Int;
            paras[1].parName = "@SenderIsMaster";
            paras[1].objValue = model.SenderIsMaster;

            paras[2].dbType = SqlDbType.VarChar;
            paras[2].parName = "@Title";
            paras[2].objValue = model.Title;

            paras[3].dbType = SqlDbType.Text;
            paras[3].parName = "@Content";
            paras[3].objValue = model.Content;

            paras[4].dbType = SqlDbType.Int;
            paras[4].parName = "@ReciverId";
            paras[4].objValue = model.ReciverId;

            paras[5].dbType = SqlDbType.Int;
            paras[5].parName = "@ReciverIsMaster";
            paras[5].objValue = model.ReciverIsMaster;

            paras[6].dbType = SqlDbType.Int;
            paras[6].parName = "@IsAll";
            paras[6].objValue = model.IsAll;

            paras[7].dbType = SqlDbType.Int;
            paras[7].parName = "@IsDel";
            paras[7].objValue = model.IsDel;

            paras[8].dbType = SqlDbType.DateTime;
            paras[8].parName = "@SendTime";
            paras[8].objValue = model.SendTime;

            paras[9].dbType = SqlDbType.Int;
            paras[9].parName = "@IsView";
            paras[9].objValue = model.IsView;

            paras[10].dbType = SqlDbType.DateTime;
            paras[10].parName = "@ViewTime";
            paras[10].objValue = model.ViewTime;

            string cmdText = "insert into Message values ({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10})";
            cmdText = string.Format(cmdText,paras[0].parName,paras[1].parName,paras[2].parName,paras[3].parName,paras[4].parName,
                paras[5].parName,paras[6].parName,paras[7].parName,paras[8].parName,paras[9].parName,
                paras[10].parName);
            db dbclass = new db();
            count = dbclass.ExcSql(cmdText,paras);

            return count;
        }

        /// <summary>
        /// 修改(修改用户查看消息状态)
        /// </summary>
        /// <param name="model">只需要提供Id,IsView,ViewTime的值</param>
        /// <returns></returns>
        public int Update(MessageModel model)
        {
            int count = 0;
            model.SendTime = DateTime.Now;
            model.IsView = 0;

            db.ParameterArry[] paras = new db.ParameterArry[3];

            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@IsView";
            paras[0].objValue = model.IsView;

            paras[1].dbType = SqlDbType.DateTime;
            paras[1].parName = "@ViewTime";
            paras[1].objValue = model.ViewTime;

            paras[2].dbType = SqlDbType.Int;
            paras[2].parName = "@Id";
            paras[2].objValue = model.Id;

            string cmdText = "update Message set IsView={0},ViewTime={1} where id={2}";
            cmdText = string.Format(cmdText, paras[0].parName, paras[1].parName, paras[2].parName);
            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);

            return count;
        }


        /// <summary>
        /// 根据filter条件查询
        /// </summary>
        /// <param name="SearchPropertys">查询的字段(字段之间用逗号隔开,如果查询所有字段用*)</param>
        /// <param name="filter">筛选条件</param>
        /// <returns></returns>
        public DataTable GetDataTable(string SearchPropertys,string filter)
        {
            DataTable dataTable = new DataTable();
            string cmdText = "select {0} from Message where {1}";
            cmdText = string.Format(cmdText,SearchPropertys,filter);
            db dbclass = new db();
            dataTable = dbclass.GetSql_DataTable(cmdText);
            return dataTable;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public int Delete(string filter)
        {
            string cmdText = "update Message set IsDel=1 where {0}";
            cmdText = string.Format(cmdText,filter);
            db dbclass = new db();

            int count = dbclass.ExcSql(cmdText);
            return count;
        }
    }
}
